package com.iot08.api.service;

import com.aliyun.tea.utils.StringUtils;
import com.iot08.api.response.SetWifiInfoResp;
import com.iot08.api.utils.TimeUtils;
import com.iot08.api.vo.request.DeviceUpdateWifiInfoReqVO;
import com.iot08.common.entity.app.SetWifiInfo;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import com.iot08.common.utils.StringUtil;
import com.iot08.common.utils.response.ResponseEnum;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author cwj
 * @description
 * @date 2024/12/19 14:19
 **/
@Slf4j
@Service
public class SetWifiService extends BaseService<SetWifiInfo> {

    /**
     * @description 保存或更新设置wifi信息
     * @author cwj
     * @param [imei, vo]
     * @return void
     * @date @time 2024/12/19 14:23
     **/
    public String saveOrUpdateSetWifiInfo(String imei, DeviceUpdateWifiInfoReqVO vo){
        SetWifiInfo setWifiInfo = querySetWifiInfo(imei,vo);
        Date date = new Date();
        log.info("系统当前时间：{},转换后的系统时间：{}",date, DateUtil.getDate(date));
        if(ObjectUtils.isEmpty(setWifiInfo)){
            setWifiInfo = new SetWifiInfo();
            setWifiInfo.setImei(imei);
            setWifiInfo.setName(vo.getName());
            setWifiInfo.setPassword(vo.getPassword());
            setWifiInfo.setSsid(vo.getSsid());
            setWifiInfo.setRemark(vo.getRemark());
            setWifiInfo.setOpenId(vo.getOpenid());
            setWifiInfo.setIsSuccess(0);
            setWifiInfo.setCreateTime(DateUtil.getDate(date));
            setWifiInfo.setUpdateTime(DateUtil.getDate(date));
            this.save(setWifiInfo);
        } else {
            updateSetWifiInfo(vo, DateUtil.getDate(date), setWifiInfo.getId());
        }
        return setWifiInfo.getId();
    }

    /**
     * @description 更新WIFI设置
     * @author cwj
     * @param [vo, date, wId]
     * @return void
     * @date @time 2024/12/25 18:37
     **/
    private void updateSetWifiInfo(DeviceUpdateWifiInfoReqVO vo, Date date, String wId) {
        Update update = new Update();
        update.set("name", vo.getName());
        update.set("openId", vo.getOpenid());
        update.set("password", vo.getPassword());
        update.set("ssid", vo.getSsid());
        if (!StringUtils.isEmpty(vo.getRemark())) {
            update.set("remark", vo.getRemark());
        }
        update.set("updateTime", date);
        this.updateById(new ObjectId(wId), update);
    }

    /**
     * @description 编辑设置的WIFI
     * @author cwj
     * @param [imei, vo]
     * @return void
     * @date @time 2024/12/25 18:28
     **/
    public ResponseMap updateSetWifiInfo(DeviceUpdateWifiInfoReqVO vo){
        //如果id为空，参数不正确
        if(StringUtils.isEmpty(vo.getWId())){
            return ResponseMap.fail(ResponseEnum.STATUS400.getCode(), ResponseEnum.STATUS400.getMsg());
        }
        updateSetWifiInfo(vo, DateUtil.getDate(new Date()), vo.getWId());
        return ResponseMap.success();
    }


    /**
     * @description 查询设置的WIFI信息
     * @author cwj
     * @param [imei]
     * @return com.iot08.common.entity.app.SetWifiInfo
     * @date @time 2024/12/19 14:40
     **/
    public SetWifiInfo querySetWifiInfo(String imei,DeviceUpdateWifiInfoReqVO vo) {
        Query query = new Query(Criteria.where("imei").is(imei).and("name").is(vo.getName()).and("ssid").is(vo.getSsid()));
        return this.findOne(query);
    }


    /**
     * @description 查询设置的wifi信息
     * @author cwj
     * @param [imei]
     * @return java.util.List<com.iot08.common.entity.app.SetWifiInfo>
     * @date @time 2024/12/20 13:38
     **/
    public List<SetWifiInfoResp> querySetWifiInfoList(String imei) {
        //查询设置成功的
        Query query = new Query(Criteria.where("imei").is(imei).and("isSuccess").is(1));
        query.fields().include("name").include("password").include("ssid").include("remark");
        return mongoTemplate.find(query, SetWifiInfoResp.class,"setWifiInfo");
    }

    /**
     * @description 设置的wifi信息
     * @author cwj
     * @param []
     * @return com.iot08.common.entity.app.SetWifiInfo
     * @date @time 2024/12/20 13:42
     **/
    public SetWifiInfo queryRecentlySetWifiInfo(String imei,String id){
        if(StringUtils.isEmpty(id)){
            /*Query query = new Query(Criteria.where("imei").is(imei).and("isSuccess").is(0));
            query.with(Sort.by(Sort.Order.desc("updateTime"))).limit(1);
            return this.findOne(query);*/
            return null;
        } else {
           return this.findById(new ObjectId(id));
        }

    }

    /**
     * @description 更新指令下发的状态
     * @author cwj
     * @param [id, isSuccess]
     * @return void
     * @date @time 2024/12/20 14:07
     **/
    public void updateSetWifiInfoIsSuccess(String id,Integer isSuccess){
        Update update = new Update();
        Date date = new Date();
        update.set("isSuccess",isSuccess);
        update.set("updateTime", DateUtil.getDate(date));
        log.info("系统当前时间：{},转换后的系统时间：{}",date, DateUtil.getDate(date));
        this.updateById(new ObjectId(id),update);
    }
    
    
    /**
     * @description 删除设备设置的wifi记录
     * @author cwj
     * @param [imei, name, ssid]
     * @return void
     * @date @time 2024/12/24 17:35
     **/
    public void deleteSetWifiByImeiName(String imei,String name,String ssid){
        Criteria criteria = Criteria.where("imei").is(imei).and("name").is(name).and("ssid").is(ssid);
        Query query = new Query(criteria);
       // 执行删除操作
        mongoTemplate.remove(query, SetWifiInfo.class);
    }
}
